package com.weishu.housing.controller;

import com.aliyuncs.exceptions.ClientException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.netflix.client.http.HttpRequest;
import com.weishu.common.core.web.domain.AjaxResult;
import com.weishu.core.entity.BusinessEntity;
import com.weishu.core.entity.UserEntity;
import com.weishu.core.service.ICfContractService;
import com.weishu.core.service.ISysUserService;
import com.weishu.core.service.ITbBusinessService;
import com.weishu.housing.sms.SmsService;
import com.weishu.housing.utils.CusAccessObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.net.URI;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 商户管理
 */
@RequestMapping("/business")
@RestController
public class BusinessController {

    @Autowired
    private ICfContractService iCfContractService;

    @Autowired
    private ITbBusinessService iTbBusinessService;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    SmsService smsService;

    private int count=0;

    //商户注册
    @PostMapping("registerBus")
    public AjaxResult registerBus(@RequestBody UserEntity sysUser){
        System.out.println(sysUser);
        AjaxResult ajaxResult = iSysUserService.registerUser(sysUser);
        return ajaxResult;
    }

    /**
     * 商户列表
     * @param pageNo
     * @param pageSize
     * @return
     */
   // @CachePut(key = "#businessList",value = "info",condition = "#businessList!=null")
    @RequestMapping("/list")
    public AjaxResult list(Integer pageNo,Integer pageSize){
        PageHelper.startPage(pageNo,pageSize);
        List<BusinessEntity> list = iTbBusinessService.listAll();
        PageInfo<BusinessEntity> info = new PageInfo<>(list);
        return AjaxResult.success(info);
    }

    /**
     * 获取短信验证码
     * @param mobile
     * @return
     * @throws ClientException
     */
    @PostMapping("/getSmsCode/{mobile}")
    public AjaxResult getSmsCode(@PathVariable String mobile, HttpServletRequest request) throws  ClientException {
        String ipAddress = CusAccessObjectUtil.getIpAddress(request);
        String[] split = ipAddress.split(",");
        System.out.println(split[0]);
        redisTemplate.opsForValue().set(split[0],ipAddress);
        count++;
        System.out.println(count);
        if (count>5){
            return AjaxResult.error("发送次数已达上限！");
        }
        if (!redisTemplate.opsForValue().setIfAbsent(mobile,mobile,2, TimeUnit.MINUTES)){
           return AjaxResult.error("请勿频繁发送！");
        }
        System.out.println("发送短信");
        smsService.sendCode(mobile);
        return AjaxResult.success("发送成功，请注意查收！");
    }

    @PostMapping("updateBusiness")
    public AjaxResult updateBusiness(@RequestBody BusinessEntity businessEntity){
        return iTbBusinessService.updateStatusById(businessEntity);
    }

    /**
     * 补全信息-》添加商户
     * @param businessEntity
     * @return
     */
    @PostMapping("addBusiness")
    public AjaxResult addBusiness(@RequestBody BusinessEntity businessEntity){
        businessEntity.setCreateTime(new Date());
        boolean save = iTbBusinessService.add(businessEntity);
        if (save){
            return AjaxResult.success("提交成功！");
        }
        return AjaxResult.error("提交失败，服务繁忙！");
    }
}
